import os.path
import threading
import keyboard
from tools.beep import beep
from tools.beep import end_beep

from tools.mouse_state_recorder import run as run__mouse_state_recorder, MouseStateRecorder
from tools.key_state_recorder import run as run__key_recorder, KeyStateRecorder
from tools.screen_recorder import run as run__screen_recorder, ScreenRecorder
from tools.abs_mouse_recorder import AbsMouseRecorder
from tools.abs_mouse_recorder import play_record as play_abs_mouse_record
from tools.abs_mouse_recorder import run as run__abs_mouse_recorder

from tools.use_key_state_move_mouse import run as run__key_move_mouse

# from tools.enum_window import get_hwnd_by_enum, get_title_bar_size, move_wind_to_left_top, bring_window_to_top
from tools.enum_window import set_genshin_window_position
from tools import ms
from tools import kk
from bdtime import Time

from tools.mouse_state_recorder import DefaultArgs
from tools.image_utils import AutoEnterAndExit

if __name__ == '__main__':
    save_file_name = 'cao_shen_pass.json'

    flag__remove_file = False
    # flag__remove_file = True

    use_screen_recorder = Fone
    # play_target = True
    # play_target = False

    # --- 是否录制完后重放一遍
    play_recorded_states = None
    # play_recorded_states = False

    tt = Time()
    mouse_state_recorder = MouseStateRecorder(tt=tt)
    key_state_recorder = KeyStateRecorder(tt=tt)
    # screen_recorder = ScreenRecorder(tt=tt, save_file_name=screen_video_dir_name, operation_file_name=save_file_name)
    screen_recorder = ScreenRecorder(tt=tt, save_file_name=None, operation_file_name=save_file_name,
                                     wait_start_flag=True, check_stop_flag=True)

    abs_mouse_recorder = AbsMouseRecorder(tt=tt)

    if flag__remove_file:
        tt.tqdm_sleep(f'准备清除已有记录[{save_file_name}]!', T=1)
        for instance in [mouse_state_recorder, key_state_recorder, screen_recorder]:
            save_file_path = instance.get_save_file_path(save_file_name)
            if os.path.exists(save_file_path):
                from tools.remove_temp_file import remove_path
                remove_path(save_file_path)
        beep()
        # exit()

    has_genshin_window = set_genshin_window_position()
    if has_genshin_window:
        auto_enter_and_exit = AutoEnterAndExit()

        if play_recorded_states is None:
            play_recorded_states = True
    else:
        if play_recorded_states is None:
            play_recorded_states = False

    exists_save_file = False
    if os.path.exists(key_state_recorder.get_save_file_path(save_file_name)):
        exists_save_file = True

    if play_target is None:
        play_target = True if exists_save_file else False
    remove_else_recoded = False
    hotkey = 'alt + `'
    # print(f'--- wait hotkey[{hotkey}]!')
    # keyboard.wait(hotkey)

    tt.sleep(0.5)
    print('--- start mouse and key recorder!')
    beep(t=0.3)

    def end_call():
        DefaultArgs.flag__stop_screen_record = True

    key_kwargs = {
        'save_file_name': save_file_name,
        'play': play_recorded_states,
        'remove_else_recoded': remove_else_recoded,
        'play_target': play_target,  # 是否播放目标文件
        'end_call': end_call,
        'key_state_recorder': key_state_recorder,
    }
    task_key = threading.Thread(target=run__key_recorder, kwargs=key_kwargs)

    key_kwargs = {
        'save_file_name': save_file_name,
        'play': play_recorded_states,
        # 'remove_else_recoded': remove_else_recoded,
        'play_target': play_target,
        # 'end_call': end_call,
        'abs_mouse_recorder': abs_mouse_recorder,
    }
    task_abs_mouse = threading.Thread(target=run__abs_mouse_recorder, kwargs=key_kwargs)

    if use_screen_recorder:
        km_kwargs = {
            'screen_recorder': screen_recorder,
        }
        task_screen_recorder = threading.Thread(target=run__screen_recorder, kwargs=km_kwargs)
        task_screen_recorder.start()

    task_abs_mouse.start()
    task_key.start()

    task_key.join()
    task_abs_mouse.join()

    end_beep()

